package com.fiberhome.ztelauncher.core.shortcut;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fiberhome.ztelauncher.core.ActionUtil;
import com.fiberhome.ztelauncher.core.FocusCarry;
import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.core.page.MultiTypeAdapter;
import com.fiberhome.ztelauncher.uilib.DateLayout;
import com.fiberhome.ztelauncher.util.BitmapUitl;
import com.fiberhome.ztelauncher.util.DisplayUtil;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataCommon;
import com.fiberhome.ztelauncher.xml.bean.LauncherLayout;
import com.fiberhome.ztelauncher.core.action.ExtraInterface;

import java.util.List;

/**
 * Created by xc on 2018/3/20.
 * 描述：
 */

public class ShortcutLayout extends LinearLayout implements View.OnClickListener, View.OnFocusChangeListener, FocusCarry {

    private final static String NAV_LOGO = "NAV_LOGO";
    private final static String NAV_SEARCH = "NAV_SEARCH";
    private final static String NAV_CLOCK = "NAV_CLOCK";
    private final static String NAV_SHOW_WIFI = "NAV_SHOW_WIFI";
    private final static String NAV_WEATHER = "NAV_WEATHER";
    private final static String NAV_USER_LOGIN = "NAV_USER_LOGIN";
    private final static String NAV_HELP = "NAV_HELP";
    private ImageView ivLogo;
    private ImageView ivSearch;
    private TextView tvSearch;
    private DateLayout datelayout;
    private ImageView tvNet;
    private ImageView tvWeather;
    private ImageView tvUser;
    private ImageView tvHelp;
    private List<LaunchDataCommon.Shortcut> shortcutList;

    private ExtraInterface mExtraInterface;
    private int tagMax;
    private View currentFocus;

    public ShortcutLayout( Context context) {
        super(context);
        initView(context);
    }

    public ShortcutLayout( Context context,  AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ShortcutLayout( Context context,  AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.shortcut_layout, this, true);
        ivLogo = (ImageView) view.findViewById(R.id.iv_logo);
        ivSearch = (ImageView) view.findViewById(R.id.iv_search);
        tvSearch = (TextView) view.findViewById(R.id.tv_search);
        datelayout = (DateLayout) view.findViewById(R.id.datelayout);
        tvNet = (ImageView) view.findViewById(R.id.tv_net);
        tvWeather = (ImageView) view.findViewById(R.id.tv_weather);
        tvUser = (ImageView) view.findViewById(R.id.tv_user);
        tvHelp = (ImageView) view.findViewById(R.id.tv_help);

        ivLogo.setOnClickListener(this);
        ivSearch.setOnClickListener(this);
        datelayout.setOnClickListener(this);
        tvNet.setOnClickListener(this);
        tvWeather.setOnClickListener(this);
        tvUser.setOnClickListener(this);
        tvHelp.setOnClickListener(this);
        ivLogo.setOnFocusChangeListener(this);
        ivSearch.setOnFocusChangeListener(this);
        datelayout.setOnFocusChangeListener(this);
        tvNet.setOnFocusChangeListener(this);
        tvWeather.setOnFocusChangeListener(this);
        tvUser.setOnFocusChangeListener(this);
        tvHelp.setOnFocusChangeListener(this);

    }

    public void setShortcutData(List<LaunchDataCommon.Shortcut> shortcutList,
                                LauncherLayout.PageCommon pageCommon, LaunchDataCommon.Navs navs) {
        setPadding(pageCommon.margin.left,0,pageCommon.margin.right,0);

        this.shortcutList = shortcutList;
        int tagI = 0;
        for (int i = 0; i < shortcutList.size(); i++) {
            LaunchDataCommon.Shortcut shortcut = shortcutList.get(i);
            if (NAV_LOGO.equals(shortcut.type)) {
                if (!TextUtils.isEmpty(shortcut.img)) {
                    ivLogo.setVisibility(VISIBLE);
                    BitmapUitl.loadBitmapTag(getContext(), ivLogo, shortcut.img);
                    ivLogo.setFocusable(shortcut.canfocus);
                    if (shortcut.canfocus) {
                        ivLogo.setTag(tagI++);
                    }
                }else {
                	ivLogo.setVisibility(GONE);
                }
            } else if (NAV_SEARCH.equals(shortcut.type)) {
                if (!TextUtils.isEmpty(shortcut.img)) {
                    ivSearch.setVisibility(VISIBLE);
                    BitmapUitl.loadBitmap(getContext(), ivSearch, shortcut.img);
                    tvSearch.setText(shortcut.name);
                    ivSearch.setFocusable(shortcut.canfocus);
                    if (shortcut.canfocus) {
                        ivSearch.setTag(tagI++);
                    }
                }

            } else if (NAV_CLOCK.equals(shortcut.type)) {
//                datelayout.setTextStyle(navs);
//                if (!TextUtils.isEmpty(shortcut.name)) {
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm");// HH:mm
//                    try {
//                        Date date = simpleDateFormat.parse(shortcut.name);
//                        datelayout.setDate(date.getTime());
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                } else {
//                    datelayout.setDate(System.currentTimeMillis());
//                }
//                datelayout.setFocusable(shortcut.canfocus);
//                if (shortcut.canfocus) {
//                    datelayout.setTag(tagI++);
//                }

            } else if (NAV_SHOW_WIFI.equals(shortcut.type)) {
                tvNet.setFocusable(shortcut.canfocus);
                setView(tvNet, shortcut.img);
                //没有配置，默认显示系统网络
                if (shortcut.canfocus) {
                    tvNet.setTag(tagI++);
                }

            } else if (NAV_WEATHER.equals(shortcut.type)) {
                tvWeather.setFocusable(shortcut.canfocus);
                if (shortcut.canfocus) {
                    tvWeather.setTag(tagI++);
                }
                setView(tvWeather, shortcut.img);

            } else if (NAV_USER_LOGIN.equals(shortcut.type)) {
                tvUser.setFocusable(shortcut.canfocus);
                if (shortcut.canfocus) {
                    tvUser.setTag(tagI++);
                }
                setView(tvUser, shortcut.img);

            } else if (NAV_HELP.equals(shortcut.type)) {
                tvHelp.setFocusable(shortcut.canfocus);
                if (shortcut.canfocus) {
                    tvHelp.setTag(tagI++);
                }
                setView(tvHelp, shortcut.img);
            }
        }
        tagMax = tagI;

        //如果没有配置时钟
        if(datelayout.hasEmpty()){
            datelayout.setTextStyle(navs);
            datelayout.setDate(System.currentTimeMillis());
        }
        ivLogo.setBackgroundResource(R.color.colorPrimary);
    }

    private void setView(ImageView tv, String imgUrl) {
        if (!TextUtils.isEmpty(imgUrl)) {
            BitmapUitl.loadBitmapTag(getContext(), tv, imgUrl);
        } else {
            tv.setVisibility(GONE);
        }
    }

    public void setItemClick(ExtraInterface extraInterface) {
        mExtraInterface = extraInterface;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_logo:
                into(NAV_LOGO);
                break;
            case R.id.iv_search:
                into(NAV_SEARCH);
                break;
            case R.id.tv_search:

                break;
            case R.id.datelayout:
                into(NAV_CLOCK);
                break;
            case R.id.tv_net:
                into(NAV_SHOW_WIFI);
                break;
            case R.id.tv_weather:
                into(NAV_WEATHER);
                break;
            case R.id.tv_user:
                into(NAV_USER_LOGIN);
                break;
            case R.id.tv_help:
                into(NAV_HELP);
                break;
            default:
                break;
        }
    }

    /**
     * 跳转
     *
     * @param type
     */
    private void into(String type) {
        for (int i = 0; i < shortcutList.size(); i++) {
            LaunchDataCommon.Shortcut shortcut = shortcutList.get(i);
            if (type.equals(shortcut.type)) {
            	ActionUtil.goTo(getContext(), shortcut.elementAction, shortcut.extraData);
//                MultiTypeAdapter.goTo(getContext(), shortcut.elementAction, shortcut.extraData);
                break;
            }
        }
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            view.setBackgroundResource(R.drawable.shape_frame);
            currentFocus = view;
        } else {
            view.setBackgroundColor(Color.parseColor("#00000000"));
        }
    }

    @Override
    public boolean carry(int direction) {
        if (direction == KeyEvent.KEYCODE_DPAD_DOWN) {
            return false;
        } else if (direction == KeyEvent.KEYCODE_DPAD_UP) {
            return true;
        } else if (direction == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (currentFocus != null) {
            	Integer tag = (Integer) currentFocus.getTag();
                return tag > 0;
            }
        } else if (direction == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (currentFocus != null) {
            	Integer tag = (Integer) currentFocus.getTag();
                return tag < tagMax;
            }
        }
        return true;
    }

    @Override
    public boolean focus(boolean hasFocus) {
        if (currentFocus != null) {
            currentFocus.requestFocus();
            return true;
        }
        return false;
    }
}
